package com.app.logrecord.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.app.logrecord.mapper.OperationMapper;
import com.app.logrecord.pojo.FieldDiffDTO;
import com.app.logrecord.pojo.ObjectDiffDTO;
import com.app.logrecord.pojo.dto.OperationPageDto;
import com.app.logrecord.pojo.po.Operation;
import com.app.logrecord.pojo.po.OperationField;
import com.app.logrecord.service.OperationFieldService;
import com.app.logrecord.service.OperationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;

/**
 * @author qiangt
 * @version 1.0
 * @date 2024/3/29 14:32
 * @description: 操作记录service实现
 */
@Validated
@RequiredArgsConstructor
@Service
public class OperationServiceImpl extends ServiceImpl<OperationMapper, Operation> implements OperationService {

    private final OperationFieldService operationFieldService;

    @Override
    public Page<Operation> pageByBusinessId(@Valid OperationPageDto pageDto) {
        Page<Operation> page = new Page<>(pageDto.getPageCurrent(), pageDto.getPageSize());
        String orderSql = pageDto.getOrderSql();
        return this.lambdaQuery()
                .eq(Operation::getBusinessId, pageDto.getBusinessId())
                .eq(Operation::getModule, pageDto.getModule())
                .eq(ObjectUtil.isNotNull(pageDto.getType()), Operation::getType, pageDto.getType())
                .eq(StrUtil.isNotBlank(pageDto.getOperatorName()), Operation::getOperatorName, pageDto.getOperatorName())
                .ge(ObjectUtil.isNotNull(pageDto.getOperationStartTime()), Operation::getCreateTime,
                        pageDto.getOperationStartTime())
                .le(ObjectUtil.isNotNull(pageDto.getOperationEndTime()), Operation::getCreateTime,
                        pageDto.getOperationEndTime())
                .last(StrUtil.isNotBlank(orderSql), orderSql)
                .orderByDesc(StrUtil.isBlank(orderSql), Operation::getCreateTime)
                .page(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(ObjectDiffDTO objectDiffDTO) {
        // 存入操作记录表
        Operation operation = new Operation();
        operation.setBusinessId(Convert.toLong(objectDiffDTO.getBusinessId()));
        operation.setModule(objectDiffDTO.getModule());
        operation.setType(objectDiffDTO.getLogOperate());
        operation.setDescription(objectDiffDTO.getDescription());
        operation.setClassBefore(objectDiffDTO.getOldClassName());
        operation.setClassAfter(objectDiffDTO.getNewClassName());
        operation.setJsonBefore(objectDiffDTO.getJsonBefore());
        operation.setJsonAfter(objectDiffDTO.getJsonAfter());
        operation.setOperatorName(objectDiffDTO.getOperatorName());
        this.save(operation);
        // 存入操作记录字段表
        List<FieldDiffDTO> fieldDiffDTOList = objectDiffDTO.getFieldDiffDTOList();
        for (FieldDiffDTO fieldDiffDTO : fieldDiffDTOList) {
            OperationField operationField = new OperationField();
            operationField.setOperationId(operation.getId());
            operationField.setFieldName(fieldDiffDTO.getFieldName());
            operationField.setFieldNameShow(fieldDiffDTO.getName());
            operationField.setChangeType(fieldDiffDTO.getEditType());
            operationField.setFieldBefore(Convert.toStr(fieldDiffDTO.getOldValue()));
            operationField.setFieldBeforeShow(Convert.toStr(fieldDiffDTO.getOldValueShow()));
            operationField.setFieldAfter(Convert.toStr(fieldDiffDTO.getNewValue()));
            operationField.setFieldAfterShow(Convert.toStr(fieldDiffDTO.getNewValueShow()));
            operationFieldService.save(operationField);
        }
    }
}
